/***************************************************************
Footer Module of Blockworld
All Threads are here.
****************************************************************
*/



;======================================================
; Draw scene    ; This draws every frame
;======================================================
SYS_BLOCKWORLD_DRAW_GL:
  glLoadIdentity()
  glClear(GL_COLOR_BUFFER_BIT)
  
  Loop, parse, BLOCKWORLD_DWL_default, `,
  {
    If(a_loopfield = ""){
      Continue
    }else{
      SYS_BLOCKWORLD_DRAW_OBJECT_ID := a_loopfield
    }
    ;so we have our OBJECT to draw:

    
    ;get infromation from our QUAD struct
    _x := NumGet(BLOCKWORLD_QUADPOSINFORMATION%SYS_BLOCKWORLD_DRAW_OBJECT_ID%,0,"Int")			    ; x
	_y := NumGet(BLOCKWORLD_QUADPOSINFORMATION%SYS_BLOCKWORLD_DRAW_OBJECT_ID%,4,"Int")			    ; y
	_w := NumGet(BLOCKWORLD_QUADPOSINFORMATION%SYS_BLOCKWORLD_DRAW_OBJECT_ID%,8,"UInt")			    ; with
	_h := NumGet(BLOCKWORLD_QUADPOSINFORMATION%SYS_BLOCKWORLD_DRAW_OBJECT_ID%,12,"UInt")	        ; height
    
    texture := BLOCKWORLD_OBJECT_TEXTURE_GET(SYS_BLOCKWORLD_DRAW_OBJECT_ID)
    
    _BLOCKWORLD_Draw(texture, _x, _y, _w, _h)
  
   }
   x1 := 100
   x2 := 200
   y1 := 300
   y2 := 400
   glColor4f(1,33,1,1)
   glBegin(GL_QUADS)
   glVertex2f(x1, y1)
   glVertex2f(x2, y1)
   glVertex2f(x2, y2)
   glVertex2f(x1, y2)
   glEnd()

  DllCall("gdi32.dll\SwapBuffers", uint, hDC)
return


;======================================================
; Handles the animations
; That mens setting the correct textures(sprite) on the 
; Objects.
;======================================================
SYS_BLOCKWORLD_ANIMATION:
   IF(BW_ANIMATION_PLAY_LIST = ""){
      BW_ANIMATION_COUNTER_ID := 0
      Return
   }
   COPY_LIST   := BW_ANIMATION_PLAY_LIST   ;because we have to modifi
   Loop, parse, COPY_LIST, `,
   {
      If(A_LoopField = ""){
         Continue
      }else{
         ID := A_LoopField
      }
      IDb   := ID -  1
      
      ;This Aniamtion Id
      MYANIM        := BW_ANIMATION_ID%ID%_ANIMATION                                    ; Animation name
      OBJECT_ID     := BW_ANIMATION_ID%ID%_OBJECT_ID                                    ; Object ID
      index         := BW_ANIMATION_ID%ID%_CNT                                          ; Counter
      AUTOEND       := BW_ANIMATION_ID%ID%_AUTOEND

      Frame_CNT := BW_ANIMATION_%MYANIM%_COUNT    ; How many frames we have?
      
      NEW_TEXTURE   := NumGet(BW_ANIMATION_%MYANIM%_TEXTURES, (index -1) * 4, "UInt")
      BLOCKWORLD_OBJECT_TEXTURE_SET(OBJECT_ID, NEW_TEXTURE)
      
      IF(index = Frame_CNT){        ; has the animation finished?
        If(AUTOEND){
           ANIMATION_REMOVE_ID(ID)
           Continue
        }else{
          ;reset the animation counter
          BW_ANIMATION_ID%ID%_CNT   := 1
        }
      }else{
         BW_ANIMATION_ID%ID%_CNT += 1      ;next frame
      }
      
   }
return

/*************************************************************
PHY2
**************************************************************
*/
BLOCKWORLD_SYS_PHY:
   ;ToolTip, Under PHY Control: %SYS_PHY_CONTROLLIST_ACTIVE%
   PHY_COLLISION_LIST()

   SYS_PHY_CONTROLLIST_GARBAGE()        ;Release all PHY locks if possible
return


/*************************************************************
Calculates the new x and y pos from the given vectors.
vector NULL means no movement.
**************************************************************
*/
BLOCKWORLD_SYS_PHY_VEC:
   PHY_EXECUTE_VECTORS()
return

BLOCKWORLD_SYS_PHY_GVY:
   PHY_GRAVITY_SET(4)
return


BLOCKWORLD_DEBUG_VIEW:
   GUICONTROL,2:,DBG_OBJECTCOUNT, DBG_OBJECTCOUNT %BLOCKWORLD_OBJECT_COUNT%
   GUICONTROL,2:,DBG_ANIMATIONPLAYLIST,  DBG_ANIMATIONPLAYLIST %BW_ANIMATION_PLAY_LIST%
   GUICONTROL,2:,DBG_BLOCKWORLD_DWL_default,  DBG_BLOCKWORLD_DWL_default %BLOCKWORLD_DWL_default%
   
   ;PHY
   GUICONTROL,2:,DBG_PHYSTATICCOUNT, DBG_PHYSTATICCOUNT %PHY_OBJECT_COUNT_static%
   GUICONTROL,2:,DBG_PHYDYNAMICCOUNT, DBG_PHYDYNAMICCOUNT %PHY_OBJECT_COUNT_dynamic%
   
   ;locked objects:
   GUICONTROL,2:,DBG_SYS_PHY_CONTROLLIST_ACTIVE, PHY LOCKED  %SYS_PHY_CONTROLLIST_ACTIVE%
   
return

/*************************************************************************
Is a object locked by PHY?
**************************************************************************
*/
SYS_PHY_CONTROLLIST_IS(object){
global SYS_PHY_CONTROLLIST_ACTIVE
   IF object in %SYS_PHY_CONTROLLIST_ACTIVE%
      return, true
   Else
      return, false
}


SYS_PHY_CONTROLLIST_ADD(obj){
global    SYS_PHY_CONTROLLIST_ACTIVE
   SYS_PHY_CONTROLLIST_ACTIVE .= obj ","    ; we add the object to the list
   sort, SYS_PHY_CONTROLLIST_ACTIVE, U      ; remove duplicates
}

/************************************************************************
;   we parse the Object list
;   and remove all objects which didn't have a collision before
*************************************************************************
*/
SYS_PHY_CONTROLLIST_GARBAGE(){
 global  
 local SYS_PHY_CONTROLLIST_TMP
   SYS_PHY_CONTROLLIST_TMP  := ""
   Loop, parse, SYS_PHY_CONTROLLIST_ACTIVE, `,
   {
      IF(A_LoopField = ""){
         Continue
      }
      IF(PHY_GET_OBJECT_COLLISION(A_LoopField)){
         SYS_PHY_CONTROLLIST_TMP .= A_LoopField ","
      }else{
         ;this Object had a collision but yet nomore
         If(!BLOCKWORLD_OBJECT_GET_BYTE(A_LoopField,"PHY_DONT_STOP_OBJECT")){
         ; PHY stops the Object now
         ; PHY_OBJECT_UPDATE(A_LoopField,"","","","" ,0,0) <--- cant use this, coz is blocked....
            sNumPut(0, PHY_QUAD%A_LoopField%,16,"Int")				;x vector
            sNumPut(0, PHY_QUAD%A_LoopField%,20,"Int")				;y vector
         }

      }
   }
   SYS_PHY_CONTROLLIST_ACTIVE := SYS_PHY_CONTROLLIST_TMP
}



GuiClose:
ExitApp
Exit:
Gui, hide
DllCall("opengl32.dll\wglMakeCurrent", UInt, 0, UInt, 0)
DllCall("opengl32.dll\wglDeleteContext", UInt, hRC)
DllCall("ReleaseDC", UInt, GLControl, UInt, hDC)
DllCall("FreeLibrary", UInt, hOpenGL)
DllCall("FreeLibrary", UInt, hGdi32)
Gdip_Shutdown(pToken)
exitapp